package com.zxj.camerademo.utils;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class PermissionUtils {

    private static final int PERMISSION_REQUEST_CODE = 100;
    private static final int PERMISSION_SETTING_CODE = 101;

    private AlertDialog permissionExplainDialog = null;
    private AlertDialog permissionSettingDialog = null;

    private PermissionUtils(){}
    private static class PermissionUtilsHolder{
        private static final PermissionUtils INSTANCE = new PermissionUtils();
    }
    public static final PermissionUtils getInstance(){
        return PermissionUtilsHolder.INSTANCE;
    }

    /**
     * 第一步. 检查权限
     */
    public void checkPermission(AppCompatActivity activity,String[] permissionList,Runnable callBack){
        boolean allGranted = true;
        for (String it : permissionList) {
            int result = ContextCompat.checkSelfPermission(activity, it);
            Log.e("PermissionUtils","检查权限:"+it +"结果:"+result);

            if (result != PackageManager.PERMISSION_GRANTED) { //没有权限
                allGranted = false;
                Log.e("PermissionUtils","没有权限");
            }
        }

        if(allGranted){ //拥有全部权限
            callBack.run();
        }else {
            startRequestPermission(activity, permissionList);
        }
    }

    /**
     * 第二步，如果用户之前拒绝过，展示需要权限的提示框，否则的话直接请求相关权限
     */
    private void startRequestPermission(AppCompatActivity activity, String[] permissionList){
        for (String it : permissionList) {
            /**
             * shouldShowRequestPermissionRationale
             * 如果应用之前请求过该权限但用户拒绝了该方法就会返回true
             *
             * 如果用户之前拒绝了权限请求并且勾选了权限请求对话框的”不再询问”，该方法会返回false，
             * 如果设备策略禁止该应用获得该权限也会返回false
             *
             */
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, it)) {
                // 向用户显示一个解释，要以异步非阻塞的方式
                // 该线程将等待用户响应！等用户看完解释后再继续尝试请求权限
                Log.e("PermissionUtils","  showPermissionExplainDialog()");
                showPermissionExplainDialog(activity, permissionList);
            }else {
                /**
                 * 当你的应用调用requestPermissions()方法时，系统会向用户展示一个标准对话框，
                 * 你的应用不能修改也不能自定义这个对话框，如果你需要给用户一些额外的信息和解释你就需要在
                 * 调用requestPermissions()之前像上面一样" 解释为什么应用需要这些权限"
                 */
                Log.e("PermissionUtils","requestPermission");
                requestPermission(activity, permissionList);
            }
        }
    }


    /**
     *  不需要向用户解释了，我们可以直接请求该权限
     *  第三步. 请求权限
     */
    private void requestPermission(AppCompatActivity activity,String[] permissionList){
        ActivityCompat.requestPermissions(activity, permissionList, PERMISSION_REQUEST_CODE);
    }


    /**
     * 当用户之前拒绝过，展示一个对话框，解释为什么需要此权限
     */
    private void showPermissionExplainDialog(final AppCompatActivity activity, final String[] permissionList){
        if (permissionExplainDialog == null) {
            permissionExplainDialog = new AlertDialog.Builder(activity).setTitle("权限申请")
                   .setMessage("您刚才拒绝了相关权限，但是现在应用需要这个权限，" +
                           "点击确定申请权限，点击取消将无法使用该功能")
                   .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                           requestPermission(activity, permissionList);
                           dialog.cancel();
                       }
                   })
                   .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                           dialog.cancel();
                       }
                   }).create();
        }
        if(permissionExplainDialog != null) {
            if (!permissionExplainDialog.isShowing()) {
                permissionExplainDialog.show();
            }
        }

    }
    /**
     * 最后一步，当用户拒绝并且勾选了不在提示，那么只能引导用户去设置页面打开权限
     */
    public void showPermissionSettingDialog(final AppCompatActivity activity){
        if (permissionSettingDialog == null) {
            permissionSettingDialog = new AlertDialog.Builder(activity)
                    .setTitle("权限设置")
                    .setMessage("您刚才拒绝了相关的权限，请到应用设置页面更改应用的权限")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                            intent.setData(uri);
                            activity.startActivityForResult(intent, PERMISSION_SETTING_CODE);
                            dialog.cancel();
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    }).create();
        }
        if(permissionSettingDialog != null){
           if(permissionSettingDialog.isShowing()){
               permissionSettingDialog.show();
           }
        }
    }
}
